package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

public class QueryRunPanel extends JPanel implements ActionListener {
  private static final long serialVersionUID = -768778137113401918L;
  
  private static final String START = "Run Query";
  private static final String PAUSE = "Pause";
  private static final String RESET = "Reset";
  
  private JButton btnRun;
  private EarthPanel pnlEarth;
  private Clock clock;
  
  public QueryRunPanel(Clock clock) {
    super();
    this.clock = clock;
    
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    setBorder(BorderFactory.createTitledBorder("Query Controls"));
//    setAlignmentX(Component.LEFT_ALIGNMENT);
    
    Dimension dim = new Dimension(190,90);
    setPreferredSize(dim);
    setMaximumSize(dim);

    btnRun = new JButton(START);
    btnRun.addActionListener(this);
    btnRun.setActionCommand(START);
    
    JButton btnReset = new JButton(RESET);
//    btnReset.setSize(new Dimension(100, 20));
//    btnReset.setPreferredSize(new Dimension(100, 20));
    btnReset.addActionListener(this);
    btnReset.setActionCommand(RESET);
    
    btnRun.setAlignmentX(Component.CENTER_ALIGNMENT);
    btnReset.setAlignmentX(Component.CENTER_ALIGNMENT);
    add(btnRun);
    add(Box.createRigidArea(new Dimension(10,10)));
    add(btnReset);
  }
    
  public void actionPerformed(ActionEvent evt) {
    if(evt.getActionCommand().equals(START)) {
      clock.start();
      setRunButton(PAUSE);
      pnlEarth.disableTooltips();
    }
    else if(evt.getActionCommand().equals(PAUSE)) {
      clock.pause();
      setRunButton(START);
      pnlEarth.enableTooltips();
    }
    else if(evt.getActionCommand().equals(RESET)) {
      clock.reset();
      setRunButton(START);
      pnlEarth.enableTooltips();
    }    
  }
  
  private void setRunButton(String buttonId) {
    btnRun.setText(buttonId);
    btnRun.setActionCommand(buttonId);
  }

}
